﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace TimeStart
{
    public partial class Home : Form
    {
        int hours, minutes, seconds, temp, count, millinow, temptotal, tglobal, th, tm, ts,runtime;
        string stemptotal = "";
        public static string[] tmex = new string[39];
        bool count2 = false, exit=false, viewbaloon=false, autocheck;
        TextWriter recordfiletext, recordfilenumeric;
        TextReader recordfilenumeric2;
        public static bool reportshown=false;

        public Home()
        {
            InitializeComponent();
        }

        private void Home_Load(object sender, EventArgs e)
        {
            int starts, startm, starth,k,n;
            string tlang, tautocheck, tstring;
            
            TextReader fsettings = new StreamReader("settings.ini");
            TextReader flang;
            tautocheck = fsettings.ReadLine();
            tlang = fsettings.ReadLine();
            fsettings.Close();
            if (tlang.Contains("eng"))
                flang = new StreamReader("LANGS\\eng.txt");
            else
                flang = new StreamReader("LANGS\\ita.txt");
            if (tautocheck.Contains("0"))
                autocheck = false;
            else
                autocheck = true;
            
            k = 0;
            n=39;
            do
            {
                
            tstring = flang.ReadLine();
            tmex[k] = tstring;
            k++;
            } while(k<n);
            flang.Close();
            fileToolStripMenuItem.Text = tmex[0];
            viewLogsToolStripMenuItem.Text = visualizzaIRegistriToolStripMenuItem.Text = tmex[1];
            esciDalProgrammaToolStripMenuItem.Text = tmex[2];
            optionsToolStripMenuItem.Text = tmex[3];
            preferencesToolStripMenuItem.Text = tmex[4];
            toolsToolStripMenuItem.Text = tmex[5];
            importingTheSystemRegistryToolStripMenuItem.Text = tmex[6];
            checkForUpdatesToolStripMenuItem.Text = tmex[7];
            donateToolStripMenuItem.Text = tmex[8];
            translateInYourLanguageToolStripMenuItem.Text = tmex[9];
            reportABugToolStripMenuItem.Text = tmex[10];
            pietrosoftSiteToolStripMenuItem.Text = tmex[11];
            changeLogToolStripMenuItem.Text = tmex[12];
            informazioniToolStripMenuItem.Text = tmex[13];
            lore.Text = tmex[14];
            lminuti.Text = tmex[15];
            lsecondi.Text = tmex[16];
            notifyIcon.BalloonTipTitle = tmex[38];
           // esciDalProgrammaToolStripMenuItem.Text = tmex[2];
            esciToolStripMenuItem.Text = tmex[2];

            runtime = 0;
            temptotal = tglobal = th = tm = ts= 0;
            if (Directory.Exists("RECORDS") == false)
                Directory.CreateDirectory("RECORDS");
            Thread.Sleep(100);
            starts = startm = starth = 0;
            millinow = DateTime.Now.Millisecond + DateTime.Now.Second * 1000 + DateTime.Now.Minute * 60 * 1000 + DateTime.Now.Hour * 60 * 60 * 1000;
            millinow = millinow - Environment.TickCount;
            starts = millinow / 1000;
            if (starts > 59)
            {
                startm = starts / 60;
                starts = starts % 60;
            }
            if (startm > 59)
            {
                starth = startm / 60;
                startm = startm % 60;
            }
            lstartinfo.Text = tmex[17] + " " + starth.ToString() + ":" + startm.ToString() + ":" + starts.ToString() + ".";
            hours = minutes = seconds = temp = count = 0;
            recordfiletext = new StreamWriter("RECORDS\\" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString() + ".txt", true);
            recordfiletext.WriteLine(DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year + " - " + tmex[18] + " " + starth.ToString() + ":" + startm.ToString());
            recordfiletext.Close();
            if (File.Exists("RECORDS\\" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + "num.info") == true)
            {
                recordfilenumeric2 = new StreamReader("RECORDS\\" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + "num.info");
                stemptotal = recordfilenumeric2.ReadLine();
                int.TryParse(stemptotal, out temptotal);
                recordfilenumeric2.Close();
            }
            if (DateTime.Now.Day == 1)
            {
                int pastmonth = DateTime.Now.Month - 1, pathyear = DateTime.Now.Year;
                if (pastmonth == 0)
                {
                    pastmonth = 12;
                    pathyear = pathyear - 1;
                }
                if (File.Exists("RECORDS\\" + pastmonth.ToString() + pathyear.ToString() + "num.info") == true)
                {
                    TextReader tlastnum = new StreamReader("RECORDS\\" + pastmonth.ToString() + pathyear.ToString() + "num.info");
                    string tlastm;
                    int tintlastnum;
                    tlastm = tlastnum.ReadLine();
                    int.TryParse(tlastm, out tintlastnum);
                    int lasth, lastm, lasts;
                    lasth = lastm = lasts = 0;
                    lasts = tintlastnum / 1000;
                    if (lasts > 59)
                    {
                        lastm = lasts / 60;
                        lasts = lasts % 60;
                    }
                    if (lastm > 59)
                    {
                        lasth = lastm / 60;
                        lastm = lastm % 60;
                    }
                    tlastnum.Close();
                    if (File.Exists("RECORDS\\" + pastmonth.ToString() + "-" + pathyear.ToString() + ".txt") == true)
                    {
                        string tex;
                        tlastnum = new StreamReader("RECORDS\\" + pastmonth.ToString() + "-" + pathyear.ToString() + ".txt");
                        tex=tlastnum.ReadToEnd();
                        tlastnum.Close();
                        if (tex.Contains(tmex[19]) == false)
                        {
                            TextWriter finalreport = new StreamWriter("RECORDS\\" + pastmonth.ToString() + "-" + pathyear.ToString() + ".txt", true);
                            string text;
                            text = "\n--------------------------------------------------------------------------------------------\n";
                            finalreport.WriteLine(text);
                            text = tmex[19] +" " + lasth + " " + tmex[14] +" , " + lastm + " " + tmex[15] + " " + tmex[20] + " " + lasts + " " + tmex[16];
                            finalreport.WriteLine(text);
                            finalreport.Close();
                        }
                    }
                    

                }
            }
            timerLoop.Enabled = true;
            timerLoop.Start();
            if (autocheck)
                TimeStart.Update.checkforupdates();
        }

        private void timerLoop_Tick(object sender, EventArgs e)
        {
            temp = System.Environment.TickCount;
            seconds = temp / 1000;
            if (seconds > 59)
            {
                minutes = seconds / 60;
                seconds = seconds % 60;  
            }
            if (minutes > 59)
            {
                hours = minutes / 60;
                minutes = minutes % 60;
            }
            if (count == 10)
            {
                string sore;
                if (hours == 1)
                    sore = tmex[21];
                else
                    sore = tmex[14];
                notifyIcon.BalloonTipText = hours.ToString() + " " + sore + ", " + minutes.ToString() + " " + tmex[15] + " " + tmex[20] + " "  + seconds.ToString() + " " + tmex[16];
                if (viewbaloon)
                {
                    notifyIcon.ShowBalloonTip(1000);
                    viewbaloon = false;
                }
                count = 0;
                if (count2 == true)
                {
                    ltwopoints.Text = ":";
                    ltwopoints2.Text = ":";
                    count2 = false;
                }
                else
                {
                    ltwopoints.Text = "";
                    ltwopoints2.Text = "";
                    count2 = true;
                }
            }
            else
                count++;
            if(hours<10)
                lhours.Text = "0" + hours.ToString();
            else
                lhours.Text = hours.ToString();
            if(minutes<10)
                lminutes.Text ="0" + minutes.ToString();
            else
                lminutes.Text = minutes.ToString();
            if(seconds<10)
                lseconds.Text = "0" + seconds.ToString();
            else
                lseconds.Text = seconds.ToString();
            tglobal = temptotal + System.Environment.TickCount;
            ts = tglobal / 1000;
            if (ts > 59)
            {
                tm = ts / 60;
                ts = ts % 60;
            }
            if (tm > 59)
            {
                th = tm / 60;
                tm = tm % 60;
            }
            ltemp.Text = tmex[22] +" " + th.ToString() + " " + tmex[14] + ", " + tm.ToString() + " " + tmex[15] + " \n" + tmex[20] + " " +  ts.ToString() + " " + tmex[16] + ".";
            //notifyIcon.Text = hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString();
            
        }

        private void Home_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.WindowsShutDown)
            {
                recordfiletext = new StreamWriter("RECORDS\\" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString() + ".txt", true);
                recordfiletext.WriteLine(DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year + " - " + tmex[24] + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ". " + tmex[25] + " " + hours + " " + tmex[14] +" " + minutes + " " + tmex[15] + " " + tmex[20] + " " + seconds + " " + tmex[16]);
                //recordfile.Flush();
                recordfiletext.Close();
                recordfilenumeric = new StreamWriter("RECORDS\\" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + "num.info", false);
                temptotal += System.Environment.TickCount;
                recordfilenumeric.Write(temptotal);
                recordfilenumeric.Close();
                Thread.Sleep(100);
            }
            else
            {
                if (!exit)
                {
                    e.Cancel = true;
                    this.Hide();
                }
                else
                {
                    DialogResult answer;
                    answer = MessageBox.Show(tmex[26], tmex[2],MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (answer == DialogResult.Yes)
                    {
                        recordfiletext = new StreamWriter("RECORDS\\" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString() + ".txt", true);
                        recordfiletext.WriteLine(DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year + " - " + tmex[27] + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute);
                        recordfiletext.Close();
                        //recordfilenumeric.Close();
                        e.Cancel = false;
                    }
                    else
                    {
                        e.Cancel = true;
                        exit = false;
                    }
                }

            }
            
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Activate();
            this.Show();
           
        }

        private void esciToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exit = true;
            Application.Exit();
        }

        private void esciDalProgrammaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exit = true;
            Application.Exit();
        }

        private void Home_Activated(object sender, EventArgs e)
        {
            this.CenterToScreen();
        }

        private void notifyIcon_MouseMove(object sender, MouseEventArgs e)
        {
            notifyIcon.BalloonTipText = hours.ToString() + ", " + minutes.ToString() + " minuti e " + seconds.ToString() + " secondi";
            viewbaloon = true;
        }

        private void runTimer_Tick(object sender, EventArgs e)
        {
            ++runtime;
        }

        private void visualizzaIRegistriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (reportshown == false)
            {
                Report freport = new Report();
                freport.Show();
                reportshown = true;
            }
        }

        private void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
        {
            this.Activate();
            this.Show();
           
        }

        private void informazioniToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About fabout = new About();
            fabout.Show();
        }

        private void pietrosoftSiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("IExplore.exe","http://www.pietrosoft.altervista.org/");
        }

        private void reportABugToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("IExplore.exe", "http://www.pietrosoft.altervista.org/timestart/bugs/");
        }

        private void translateInYourLanguageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("IExplore.exe", "http://www.pietrosoft.altervista.org/timestart/translate/");
        }

        private void donateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("IExplore.exe","http://www.pietrosoft.altervista.org/timestart/donate/");
        }

        private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Update fupdate = new Update();
            fupdate.Show();
        }

        private void viewLogsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (reportshown == false)
            {
                Report freport = new Report();
                freport.Show();
                reportshown = true;
            }
        }

        private void importingTheSystemRegistryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Import fimport = new Import();
            fimport.Show();
        }

        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Preferences fpreferences = new Preferences();
            fpreferences.Show();
        }

        private void changeLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangeLog fchangelog = new ChangeLog();
            fchangelog.Show();
        }
    }
}
